package com.tfd.blog.service;

import com.tfd.blog.domain.Article;
import com.tfd.blog.exception.BlogException;
import com.tfd.blog.exception.BlogExceptionCode;
import com.tfd.blog.repository.ArticleRepository;
import com.tfd.blog.utlis.BlogConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author TangFD@HF 2019/5/8
 */
@Service
public class ArticleService {

    @Resource
    private ArticleRepository articleRepository;
    @Resource
    private LabelService labelService;

    /**
     * 保存文章和标签数据
     *
     * @param article 文章
     * @return 文章主键Id
     */
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public String save(Article article) {
        if (article == null) {
            throw new BlogException(BlogExceptionCode.ILLEGAL_ARGUMENT, "article not be null!");
        }

        Date date = new Date();
        if (StringUtils.isEmpty(article.getId())) {
            article.setCreateDate(date);
        }

        String status = article.getStatus();
        if (BlogConstants.BLOG_STATUS_PUBLISH.equals(status)) {
            if (article.getPublishDate() == null) {
                article.setPublishDate(date);
            }
        } else {
            article.setStatus(BlogConstants.BLOG_STATUS_DRAFT);
        }

        article.setUpdateDate(date);
        articleRepository.save(article);
        String articleId = article.getId();
        labelService.save(articleId, article.getLabels());

        return articleId;
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public void updateStatus(String id, String status) {
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)) {
            throw new IllegalArgumentException("id and status not be empty!");
        }
        Article article = articleRepository.findById(id).get();
        if (StringUtils.isEmpty(article.getId())) {
            throw new IllegalArgumentException("article not exist by id [" + id + "]!");
        }

        article.setStatus(status);
        simpleSave(article);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public String simpleSave(Article article) {
        article.setUpdateDate(new Date());
        articleRepository.save(article);
        return article.getId();
    }

    public Article getPreArticleId(Date publishDate) {
        if (publishDate == null) {
            throw new IllegalArgumentException("publishDate not be empty!");
        }

        Object object = articleRepository.getPreArticleId(publishDate);
        if (object == null) {
            return null;
        }

        Object[] objects = (Object[]) object;
        Article article = new Article();
        article.setId(String.valueOf(objects[0]));
        article.setTitle(String.valueOf(objects[1]));
        return article;
    }

    public Article getNextArticleId(Date publishDate) {
        if (publishDate == null) {
            throw new IllegalArgumentException("publishDate not be empty!");
        }

        Object object = articleRepository.getNextArticleId(publishDate);
        if (object == null) {
            return null;
        }

        Object[] objects = (Object[]) object;
        Article article = new Article();
        article.setId(String.valueOf(objects[0]));
        article.setTitle(String.valueOf(objects[1]));
        return article;
    }
}
